#include "OneDirectionList.h"

OneDirectionList::OneDirectionList()
: head(nullptr)
{
    init();
}
OneDirectionList::~OneDirectionList() {
    deinit();
}

void OneDirectionList::init(){
    int values[] = {
        1, 2, 3, 4, 5, 6, 7
    };
    struct Node * tail = nullptr;
    for(auto v : values ){
        if(head == nullptr){
            head = tail = new Node(v);
        }
        else{
            tail->next =  new Node(v);
            tail = tail->next;
        }
    }
}
void OneDirectionList::deinit(){
    while(head != nullptr){
        struct Node * cur =  head;
        head = head->next;
        delete cur;
    }
}
void OneDirectionList::print(){
    struct Node * tail  = head;
    while(tail != nullptr){
        std::cout << tail->val << std::endl;
        tail = tail->next;
    }
}

void OneDirectionList::run() {
    OneDirectionList list;
    list.print();
    list.reversePrint();
}
void OneDirectionList::reversePrint(){
    std::cout << "reversePrint:" << std::endl;
    reversePrint(head);
}
void OneDirectionList::reversePrint(struct Node * node){
    if(node != nullptr){
        reversePrint(node->next);
        std::cout << node->val << std::endl;
    }
}
